package com.example.enews.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.enews.R
import com.example.enews.models.MatchItem

// MatchAdapter.kt
class MatchAdapter(private var matches: List<MatchItem>) :
    RecyclerView.Adapter<MatchAdapter.MatchViewHolder>() {
    fun submitList(newList: List<MatchItem>) {
        val diffCallback = object : DiffUtil.Callback() {
            override fun getOldListSize() = matches.size
            override fun getNewListSize() = newList.size
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                matches[oldItemPosition].eventId == newList[newItemPosition].eventId
            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                matches[oldItemPosition] == newList[newItemPosition]
        }
        val diffResult = DiffUtil.calculateDiff(diffCallback)
        matches = newList
        diffResult.dispatchUpdatesTo(this)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MatchViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_match, parent, false)
        return MatchViewHolder(view)
    }

    override fun onBindViewHolder(holder: MatchViewHolder, position: Int) {
        val item = matches[position]
        holder.bind(item)
    }

    override fun getItemCount() = matches.size

    class MatchViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvEvent = itemView.findViewById<TextView>(R.id.tv_event)
        private val tvTime = itemView.findViewById<TextView>(R.id.tv_time)
        private val tvTeam1 = itemView.findViewById<TextView>(R.id.tv_team1)
        private val tvTeam2 = itemView.findViewById<TextView>(R.id.tv_team2)
        private val tvFormat = itemView.findViewById<TextView>(R.id.tv_format)
        private val tvStatus = itemView.findViewById<TextView>(R.id.tv_status)
        private var dataList = emptyList<MatchItem>()
        fun bind(item: MatchItem) {
            tvEvent.text = item.eventName
            tvTime.text = item.matchTime
            tvTeam1.text = item.team1
            tvTeam2.text = item.team2
            tvFormat.text = item.format
            tvStatus.text = if (item.isCompleted) "已结束" else "未开始"
            val context = itemView.context
            val isDarkTheme = context.resources.getBoolean(R.bool.is_dark_theme)
            when {
                item.isCompleted -> {
                    tvStatus.text = "已结束"
                    tvStatus.setTextColor(ContextCompat.getColor(context,
                        if (isDarkTheme) R.color.gray_dark else R.color.gray))
                    itemView.alpha = 0.7f
                }
                item.isLive -> {
                    tvStatus.text = "进行中"
                    tvStatus.setTextColor(ContextCompat.getColor(context,
                        if (isDarkTheme) R.color.red_dark else R.color.red))
                    itemView.alpha = 1.0f
                }
                else -> {
                    tvStatus.text = "未开始"
                    tvStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    itemView.alpha = 1.0f
                }
            }

        }
    }
    companion object {
        private var dataList = emptyList<MatchItem>()
    }

}
